package com.xiaodou.mjf.application.core.service.impl;

import com.xiaodou.mjf.application.core.repository.InvestCrowdLimitRepository;
import com.xiaodou.mjf.application.core.service.InvestCrowdLimitService;
import com.xiaodou.mjf.entity.core.InvestCrowdLimit;
import com.xiaodou.mjf.infrastructure.common.app.JpaSimpleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import javax.transaction.Transactional;
import java.util.List;

/**
 * InvestCrowdLimit业务访问实现
 *
 * @author XiaoDou.com Code Generator
 * @version 1.0 2014-10-29 17:37:52
 */
@Service
@Transactional
public class InvestCrowdLimitServiceImpl extends JpaSimpleService<InvestCrowdLimit, String> implements InvestCrowdLimitService {

    Logger logger = LoggerFactory.getLogger(InvestCrowdLimitServiceImpl.class);

    @Resource
    InvestCrowdLimitRepository investCrowdLimitRepository;

    @Override
    protected JpaRepository<InvestCrowdLimit, String> getRepository() {
        return investCrowdLimitRepository;
    }

    @Override
    public InvestCrowdLimit findByCrowdNo(String crowdNo) {
        return investCrowdLimitRepository.findByCrowdNo(crowdNo);
    }

    @Override
    public Page<InvestCrowdLimit> page(InvestCrowdLimit entity) {
        return investCrowdLimitRepository.findAll((root, query, cb) -> {
            Predicate p2 = cb.like(root.get("crowdName").as(String.class), "%" + entity.getCrowdName() + "%");
            return query.where(p2).getRestriction();
        }, preparePage(entity, new Sort(new Sort.Order(Sort.Direction.DESC, "balance"))));
    }

    @Override
    public List<InvestCrowdLimit> listNow(Integer status) {
        // 取出一堆
        return investCrowdLimitRepository.findByStatus(status);
    }

    @Override
    public Page<InvestCrowdLimit> auditPage(InvestCrowdLimit entity) {
        return investCrowdLimitRepository.findAll((root, query, cb) -> {
            Predicate p1 = cb.equal(root.get("status").as(Integer.class), 2);
            Predicate p2 = cb.like(root.get("crowdName").as(String.class), "%" + entity.getCrowdName() + "%");
            return query.where(p1, p2).getRestriction();
        }, preparePage(entity, new Sort(new Sort.Order(Sort.Direction.DESC, "balance"))));
    }

    @Override
    public List<InvestCrowdLimit> findByStatus(Integer status) {
        return investCrowdLimitRepository.findByStatus(status);
    }
}
